package com.example.myapplication.ui.dashboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.ui.home.entity.Data;
import com.example.myapplication.ui.study.StudyFragment;

import java.util.List;

public class DashAdsAdapter extends RecyclerView.Adapter<DashAdsAdapter.AdsViewHolder> {

    private List<Data> adsList;

    public DashAdsAdapter(List<Data> adsList) {
        this.adsList = adsList;
        listener = listener;
    }

    // 定义一个点击事件监听器接口
    public interface OnItemClickListener {
        void onItemClick(Data data, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    // 保存点击监听器的实例
    private OnItemClickListener listener;

    @NonNull
    @Override
    public AdsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("打印1","");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ad, parent, false);
        return new AdsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdsViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindData(adsList.get(position),holder.itemView.getContext());

        Data data = adsList.get(position);
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    Log.d("点击1","");
                    listener.onItemClick(data,position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return adsList.size();
    }

    public static class AdsViewHolder extends RecyclerView.ViewHolder {

        private ImageView adImageView;
        private TextView textView;


        public AdsViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.recycle_view_tvs);
            adImageView = itemView.findViewById(R.id.imageViewAd);

        }

        public void bindData(Data data,
                             Context context
                            ) {
            // 加载广告图片，这里需要使用图片加载库，如Glide或Picasso

            Glide.with(context)
                    .load(R.drawable.icon_1)
                    .into(adImageView);

            textView.setText(data.getContent());


        }

    }
}